package exercicio2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.faces.context.RequestMap;

@WebServlet("/recados")
public class LivroRecadosServlet extends HttpServlet {

	private static final long serialVersionUID = 8114006134825758637L;
	
	private List<String> recados = new ArrayList<String>(); 

	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		try {
			String nome = request.getParameter("nome");
			String recado = request.getParameter("recado");
			if (nome != null && recado != null)
				recados.add(nome + " - " + recado);
			PrintWriter out;
			out = response.getWriter();
			response.setContentType("text/html");
			
			out.println("<html><body>");
			out.println("<h3>Livro de Recados:</h3>");
			out.println("<html><body>");
			if (recados != null) {
				Iterator<String> it = recados.iterator();
				while (it.hasNext()) {
					String r = it.next();
					out.println(r + "<br />");
				} 
			}
			out.println("-----  <br />");
			out.println("Deixe seu recado: <br />");
			out.println("<form name='form1' method='get' action='http://localhost:8080/MatheusOliveira_Servlet_JSP/recados'>");
			out.println("Nome: <input type='text' name='nome'>");
			out.println("Recado: <input type='text' name='recado'>");
			out.println("<input type='submit' value='Submit'>");
			out.println("</form>"); 
			out.println("</body></html>");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) {

	}
}
